<?php
class Ziying extends Data_Item
{
    public function __construct($zyId = NULL)
   {
       $this->_commTableName = 'mod_ziying';
       $this->_commPrimaryKey = 'id';
       $this->_zyId = (int) $zyId;
       $this->_nid     = $this->_zyId;
   }
    // 自营详情
    public function detail ($zyId = NULL)
   {
       $zyId = $zyId > 0 ? $zyId : $this->_zyId;
       $detail  = parent::detail($zyId);
       $detail['images'] = explode('|', Arr::get($detail, 'images'));
       return $detail;
   }
   // 删除自营项目
    public function remove ($zyId = NULL)
   {
       $zyId = $zyId > 0 ? $zyId : $this->_zyId;
       $this->update(array(
              'changed' => DB::expr('unix_timestamp(now())'),
              'status'  => 'INVALID',
              ), array('id', '=', $zyId));
   }
   // 自营项目搜索
    public function search ($uid = NULL, $offset = 0, $limit = 10)
   {
       $count = DB::select(array('count("id")', 'counts'))
       ->from('mod_ziying')
       ->where('status', '=', 'VALID');

       $query = DB::select()
       ->from('mod_ziying')
       ->where('status', '=', 'VALID');
                                       
       if ($limit > 0)
       {$query->offset($offset)->limit($limit);}

       if ($uid > 0)
       {
       $count->where('uid', '=', $uid);
       $query->where('uid', '=', $uid);
       }

       $counts = $count->execute()->get('counts');
       $query  = $query->execute();

       $ziyings = array();
       foreach ($query as $rs)
       {$ziyings[] = $rs;}

       return array($counts, $ziyings);
   }

    public function getLeftPersonOfDays ()

   {

 	$freeDays = $this->getByKey('freetimeDays');
 	$persons  = $this->getPersonOfDays();
 	$freeDays = explode(',', $freeDays);
 	$days     = array();
 	$today    = strtotime(date("Y-m-d"));
 	foreach ($freeDays as $d)
 	{
 	 $d = trim($d);
 	 if (! empty($d))
 	 $days[] = $d;

 	}
        
 	//屏蔽今天之前的服务时间

 	foreach ($days as $k => $day)
 	{
 	 $t = strtotime($day);
 	 if ($t < $today)
 	 unset($days[$k]);
 	}
        $query = DB::select('startDay', 'stopDay', 'persons')
 	->from('app_order')
 	->where('goodsType', '=', 'ZIYING')
 	->where('goodsId', '=', $this->_zyId)
 	->where('status', '=', 3)
 	->execute();

 	$ordDays = array();

 	//取回app_order表中该订单的每个预定日期下的预定人数

 	foreach ($query as $rs)
 	{
 	 $day = $rs['startDay'];
 	 
         if (isset($ordDays[$day]))
 	 $ordDays[$day] += $rs['persons'];
 	 else 
 	 $ordDays[$day] = $rs['persons'];

 	}
 	$ret = array();
 	foreach ($days as $k => $day)
 	{
 	 if (in_array($day, array_keys($ordDays)))
 	{
 	   if ($persons > $ordDays[$day])
 	   $ret[$day] = $persons[$day] - $ordDays[$day];
 	}
 	else
 	   $ret[$day] = $persons[$day];
 	}

 	return $ret;
 	}

 	public function getByKey($key)
 	{
 	 if (empty($this->_info))
 	{ 
 	  $this->_info = DB::select()
 	  ->from('mod_ziying')
 	  ->where('id', '=', $this->_nid)
 	  ->execute()
 	  ->current();
 	}
 	  return Arr::get($this->_info, $key);
 	}

        public function getPersonOfDays()
 	{
 		if (empty($this->_info))
 		{
 	           $this->_info = DB::select()
 		   ->from('mod_ziying')
 		   ->where('id', '=', $this->_nid)
 		   ->execute()
 		   ->current();
 		}
 	
 		$perdetail = Arr::get($this->_info, "personDetail");
 		
 		$perDays=array();
 		if(!empty($perdetail)){
 		  $perdetail=explode(',',$perdetail);
 		
 		  foreach( $perdetail as $v){
 		    if(!empty($v)){
 		      $per=explode(':',$v);
 	              $per[0]=trim($per[0]);
 		      $per[1]=trim($per[1]);
 		      $perDays[$per[0]]=$per[1];
 		     }
 		  }
 		}
 		return $perDays;
 	}

 	public function getPriceOfDays()
 	{
 		if (empty($this->_info))
 		{
 		   $this->_info = DB::select()
 		   ->from('mod_ziying')
 		   ->where('id', '=', $this->_nid)
 		   ->execute()
 		   ->current();
 		}
 	
 		$pdetail = Arr::get($this->_info, "priceDetail");
 			
 		$pDays=array();
 		if(!empty($pdetail)){
 		  $pdetail=explode(',',$pdetail);
 				
 		  foreach( $pdetail as $v){
 		    if(!empty($v)){
 		      $p=explode(':',$v);
 		      $p[0]=trim($p[0]);
 		      $p[1]=trim($p[1]);
 		      $pDays[$p[0]]=$p[1];
 		    }
 		  }
                  $today=strtotime(date("Y-m-d"));
 		  foreach ($pDays as $d=>$p){
 		    $dtime=strtotime($d);
 		    if($dtime<$today)
 		      unset($pDays[$d]);
 	          }
 		}
 		return $pDays;
 	}
     
        public function getPriceByDay($day=null)
 	{
 		$price=$this->getPriceOfDays();
 		return $price[$day];
 	}
 	public function getStopDay($startDay=null)
 	{
 		$days=$this->getByKey('days');
 		$startDay=strtotime($startDay);
 		$stopDay=date('Y-m-d',$startDay+$days*3600*24);
 		return $stopDay;
 	}
 	public function getGuiderUid()
 	{
 		return 1;
 	}
 	public function getName()
 	{
 		return $this->getByKey('name');
 	}
 	public function getMoney ($startDay, $stopDay, $persons)
 	{
 		$price   = $this->getPriceByDay($startDay);
 		$days    = Util_Date::days($startDay, $stopDay);
 	
 		// 活动订单价格是 单价*人头数
 		return $price * $persons * 100;
 	}
 	public function updateCountsOrders ($n = NULL)
 	{
 		// $n 参数作废
 	
 		// 获取实际订单数
 		$counts = DB::select(array('count("id")', 'counts'))
 		->from('app_order')
 		->where('goodsType', '=', 'ZIYING')
 		->where('goodsId', '=', $this->_zyId)
 		->where('status', 'in', array(1, 3, 7, 99))
 		->execute()
 		->get('counts');
 	
 		DB::update('mod_ziying')
 		->set(array('orders' => $counts))
 		->where('id', '=', $this->_zyId)
 		->execute();
 	}
        public function getPrice ($humanRead = FALSE, $alldays = TRUE)
 	{
 		$n  = $this->getByKey('price');
 		if ($humanRead)
 		   return number_format($n / 100, 2);
 		else
 		   return $n;
 	}

}
